package com.demo.java8Stream;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class TestGroupBy {
    @Data
    public static class User {
        private Integer id;
        private Integer schoolId;
        private String userName;
        private String edu;
        private double price;
    }

    public static List<TestGroupBy.User> users = new ArrayList<>();

    static {
        TestGroupBy.User u1 = new TestGroupBy.User();
        u1.setId(1001);
        u1.setSchoolId(100);
        u1.setUserName("小1");
        u1.setEdu("001");
        u1.setPrice(0.01);

        TestGroupBy.User u2 = new TestGroupBy.User();
        u2.setId(1002);
        u2.setSchoolId(100);
        u2.setUserName("小2");
        u2.setEdu("002");
        u2.setPrice(0.20);

        TestGroupBy.User u3 = new TestGroupBy.User();
        u3.setId(2010);
        u3.setSchoolId(200);
        u3.setUserName("小3");
        u3.setEdu("001");
        u3.setPrice(3.00);

        TestGroupBy.User u4 = new TestGroupBy.User();
        u4.setId(3001);
        u4.setSchoolId(300);
        u4.setEdu("001");
        u4.setPrice(40.0);

        users.add(u1);
        users.add(u2);
        users.add(u3);
        users.add(u4);
    }

    public static void main(String[] args) {
        Map<String,List <Integer>> collect = users.stream().collect(Collectors.groupingBy(TestGroupBy.User::getEdu,
                //第二个参数对Map的value进行处理（映射）
                Collectors.mapping(User::getId, Collectors.toList())));
        System.out.printf("collect"+collect);
        List<Integer> integers = collect.get("001");
        System.err.printf("integers"+integers);
    }
}